package com.cnfangmao.masterdata.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.dto.BasicDataRuleDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataRule;
import com.cnfangmao.masterdata.service.BasicDataRuleService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 主数据字段控制规则定义 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Api(tags = "0-9、配置-规则管理")
@RestController
@RequestMapping("/basicDataRule")
public class BasicDataRuleController {
	
	private static final Logger logger = LoggerFactory.getLogger(BasicDataRuleController.class);

	@Autowired
	private BasicDataRuleService basicDataRuleService;

	/**
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "创建主数据字段规则", notes = "创建主数据字段规则")
	@ApiImplicitParam(name = "dto", value = "主数据字段规则", required = true, paramType = "body", dataType = "BasicDataRuleDto")
	@PostMapping("/create.do")
	public ResBody<String> create(@RequestBody BasicDataRuleDto dto) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (dto == null || dto.getRuleName() == null || dto.getClassFullName() == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("主数据字段规则名称或者实现服务类名为空");
				return resBody;
			}
			if (basicDataRuleService.getBasicDateRuleByRuleName(dto.getRuleName()) != null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("主数据字段规则名称已经存在");
				return resBody;
			}

			// dto.setMappingId(SnowFlakeUtils.generator());
			BasicDataRule basicDataRule = ModelUtils.parseObject(dto, BasicDataRule.class, "create");
			basicDataRuleService.save(basicDataRule);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
		} catch (Exception e) {
			logger.error(String.format("run create error in BasicDataRuleController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "更新主数据字段规则", notes = "更新主数据字段规则")
	@ApiImplicitParam(name = "dto", value = "主数据字段规则", required = true, paramType = "body", dataType = "BasicDataRuleDto")
	@PostMapping("/update.do")
	public ResBody<String> update(@RequestBody BasicDataRuleDto dto) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (dto == null || dto.getRuleId() == null || dto.getRuleName() == null || dto.getClassFullName() == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("主数据字段规则ID, 主数据字段规则名称或者实现服务类名为空");
				return resBody;
			}

			BasicDataRule basicDataRule = ModelUtils.parseObject(dto, BasicDataRule.class, "update");
			basicDataRuleService.updateById(basicDataRule);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("更新成功");
		} catch (Exception e) {
			logger.error(String.format("run update error in BasicDataRuleController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "获取主数据字段规则", notes = "获取主数据字段规则")
	@ApiImplicitParam(name = "id", value = "主数据字段规则的ID", required = true, paramType = "query", dataType = "string")
	@GetMapping("/getById.do")
	public ResBody<BasicDataRule> getById(@RequestParam(value = "id") String id) {
		ResBody<BasicDataRule> resBody = new ResBody<BasicDataRule>();
		try {
			if (id == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，主数据字段规则的ID为空");
				return resBody;
			}
			BasicDataRule basicDataRule = basicDataRuleService.getById(id);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(basicDataRule);
		} catch (Exception e) {
			logger.error(String.format("run getById error in BasicDataRuleController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除主数据字段规则")
	@ApiImplicitParam(name = "ids", value = "主数据字段规则的ID列表", required = true, allowMultiple = true, paramType = "query", dataType = "string")
	@PostMapping(value = "/delete.do")
	public ResBody<String> delete(@RequestParam(value = "ids") List<String> ids) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (ids == null || ids.size() == 0) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，主数据字段规则的ID为空");
				return resBody;
			}
			
			basicDataRuleService.removeByIds(ids);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
		} catch (Exception e) {
			logger.error(String.format("run delete error in BasicDataRuleController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @param searchDto
	 * @return
	 */
	@ApiOperation(value = "获取主数据字段规则列表", notes = "获取主数据字段规则列表")
	@ApiImplicitParam(name = "searchDto", value = "主数据字段规则查询条件", required = true, paramType = "body", dataType = "GeneralSearchDto«BasicDataRuleDto»")
	@PostMapping("/search.do")
	public ResBody<GeneralPageVo<BasicDataRule>> search(@RequestBody GeneralSearchDto<BasicDataRuleDto> searchDto) {
		ResBody<GeneralPageVo<BasicDataRule>> resBody = new ResBody<GeneralPageVo<BasicDataRule>>();
		try {
			if (searchDto == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，值为空");
				return resBody;
			}
			
			GeneralPageVo<BasicDataRule> page = basicDataRuleService.getBasicDateRulesByPage(searchDto);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(page);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run search error in BasicDataRuleController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取所有主数据字段规则列表", notes = "获取所有主数据字段规则列表")
	@GetMapping("/listAll.do")
	public ResBody<List<BasicDataRule>> listAll() {
		ResBody<List<BasicDataRule>> resBody = new ResBody<List<BasicDataRule>>();
		try {
			List<BasicDataRule> basicDataRules = basicDataRuleService.list();

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(basicDataRules);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run listAll error in BasicDataRuleController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
}
